﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using Microsoft.Win32;


namespace ProxySwitch
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // ////////////////////////
        // Start System Tray Code
        // ////////////////////////

        private void MainWindow_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void setProxyServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ThreadStart process = new ThreadStart(ThreadJobTray);
            Thread thread = new Thread(process);
            thread.Start();

            for (int i = 0; i < 1; i++)
            {
                // Thread 3 Execute
                if (File.Exists("Config.prsw"))
                {
                    MessageBox.Show("Internet settings have now been configured for a proxy!", "Successful!",
                        MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("The configuration file could not be found! Please contact the system administrator", "Oh dear!",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Thread.Sleep(1000);
                }
            }
        }

        static void ThreadJobTray()
        {
            for (int i = 0; i < 1; i++)
            {
                // Thread 4 Execute
                if (File.Exists("config.prsw"))
                {
                    int counter = 0;
                    string ProxyAddress;

                    System.IO.StreamReader file =
                       new System.IO.StreamReader("config.prsw");
                    while ((ProxyAddress = file.ReadLine()) != null)
                    {
                        RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
                        registry.SetValue("ProxyEnable", 1);
                        registry.SetValue("ProxyServer", (ProxyAddress));
                        counter++;
                    }
                    file.Close();
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }

        private void removeProxyServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
            registry.SetValue("ProxyEnable", 0);

            MessageBox.Show("Internet settings have now been configured for a home network!", "Successful!",
                            MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void aboutProxySwitchToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutWindow About = new AboutWindow();
            About.Show();
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}